package com.tnstart.android.control.light;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.tnstart.android.control.R;
import com.tnstart.android.control.light.fragments.LightListFragment;
import com.tnstart.android.control.light.fragments.ModeListFragment;

import java.net.URLEncoder;


public class LightMainActivity extends BaseActivity {

    public LightMainActivity() {
        super(R.string.light_main);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        setContentView(R.layout.content_frame);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame, new LightListFragment())
                .commit();

        getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
        getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
        getSlidingMenu().setRightBehindOffset(300);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.menu_frame_two, new ModeListFragment())
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.github:
                Util.goToUrl(this, "www.tang-ni.com");
                return true;
            case R.id.about:
                new AlertDialog.Builder(this)
                        .setTitle(R.string.about)
                        .setMessage(Html.fromHtml(getString(R.string.about_msg)))
                        .show();
                break;
            case R.id.licenses:
                new AlertDialog.Builder(this)
                        .setTitle(R.string.licenses)
                        .setMessage(Html.fromHtml(getString(R.string.tnstart_license)))
                        .show();
                break;
            case R.id.contact:
                final Intent email = new Intent(Intent.ACTION_SENDTO);
                String uriText = "mailto:wanghuapeng@gmail.com" +
                        "?subject=" + URLEncoder.encode("灯控软件意见by android");
                email.setData(Uri.parse(uriText));
                try {
                    startActivity(email);
                } catch (Exception e) {
                    Toast.makeText(this, R.string.no_email, Toast.LENGTH_SHORT).show();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.light_menu, menu);
        return true;
    }

}
